#ifndef OHOS_VIDEO_RENDER_OHOS_H_
#define OHOS_VIDEO_RENDER_OHOS_H_

#include <memory>
#include <ace/xcomponent/native_interface_xcomponent.h>
#include <native_window/external_window.h>
#include "ohos_video_render_base.h"
#include "rtc_base/thread.h"
#include "api/video/video_sink_interface.h"
#include "api/task_queue/pending_task_safety_flag.h"
#include "api/video/video_scale_mode.h"
#include "api/video/video_render_interface.h"

namespace webrtc {
typedef std::function<void()> RenderThreadPostTask;

class VideoRenderOhos : public VideoRenderInterface {
public:
  VideoRenderOhos();
  static std::string GetXComponentId(OH_NativeXComponent *component);

  void AttachHandle(void* handle) override;
  void AttachWindow(OH_NativeXComponent *nativeXComponent, NativeWindow *nativeWindow);
  bool Init(void* window, void* share_context, int width, int height) override;
  void SizeChange(int width, int height) override;
  void ClearColor(const std::vector<float>& rgba_color) override;
  void Release() override;
  void SetScaleType(webrtc::VideoScaleMode h_match_scale_type,
                    webrtc::VideoScaleMode h_mismatch_scale_type,
                    webrtc::VideoScaleMode v_match_scale_type,
                    webrtc::VideoScaleMode v_mismatch_scale_type) override;
  void SetMirror(bool mirror) override;
  bool GetMirror() const override;
  std::string GetRenderId() const override;
  bool Inited() const override;

  void OnFrame(const webrtc::VideoFrame& frame) override;
  
  void AddRenderListener(std::shared_ptr<VideoRenderListener> listener) override;
  void RemoveRenderListener(std::shared_ptr<VideoRenderListener> listener) override;

private:
    std::string render_id_ = "";
    OH_NativeXComponent* native_xcomponent_ = nullptr;
    NativeWindow* native_window_ = nullptr;

    std::shared_ptr<VideoRenderInterface> render_;
    std::unique_ptr<rtc::Thread> render_thread_;
    std::unique_ptr<OH_NativeXComponent_Callback> render_callback_;
    std::atomic<bool> is_render_working_;

    void RunOnRenderThread(const std::function<void()> &function);
    void StartRenderThread();
    void StopRenderThread();
};
}

#endif //OHOS_VIDEO_RENDER_OHOS_H_
